﻿using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace HMIWebDisplay
{

    public class DisplayElement
    {
        public string DisplayElementID { get; private set; }
        public string DisplayBindingID { get; private set; }
        
        public static IEnumerable<DisplayElement> ParseXDocument(XDocument xDocument)
        {
            return xDocument.Descendants("div")
                .Select(x =>
                    {
                        var id = x.Attribute("id");
                        var hdxproperties = x.Attribute("hdxproperties");
                        if (id != null && hdxproperties != null && hdxproperties.Value.Contains("HDXBINDINGID"))
                            return new DisplayElement
                                {
                                    DisplayElementID = id.Value,
                                    DisplayBindingID =
                                        Regex.Replace(hdxproperties.Value, @".+HDXBINDINGID:(\d+);.+", @"$1")
                                };
                        return null;
                    })
                .Where(x => x != null);
        }

    }
}
